package com.IO.ReadAndWrite;

import java.io.*;

/***
 * @ClassName: BufferedRW
 * @Description: 使用字符缓冲流
 * @Auther: 送一一块二向箔
 * @Date: 12:27 2022/2/7
 * @version : V1.0
 */
public class BufferedRW {
    public static void main(String[] args) {
        read();
        write();
    }

    //普通读取
    public static void read() {
        try {
            //创建缓冲流
            FileReader fr = new FileReader("d:\\aaa.txt");
            BufferedReader br = new BufferedReader(fr);
            //读取,2个字符的缓冲区
            char[] buf = new char[1024];
            int data = 0;
            while (((data = br.read(buf)) != -1)) {
                System.out.println(new String(buf, 0, data));
            }
            fr.close();
            br.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //按照行读取
    public static void read1() {
        try {
            //创建缓冲流
            FileReader fr = new FileReader("d:\\aaa.txt");
            BufferedReader br = new BufferedReader(fr);
            //读取,2个字符的缓冲区
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            fr.close();
            br.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void write() {
        try {
            //创建缓冲流
            FileWriter fw = new FileWriter("d:\\ddd.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("大家好");
            //换行
            bw.newLine();
            bw.write("好好学习，天天向上");
            bw.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
